﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;

namespace CYTS.Aoyou.Framework.Utility
{
    /// <summary>
    /// 邮件帮助类
    /// </summary>
    public class EmailHelper
    {

        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="From">发件人的电子邮件地址。</param>
        /// <param name="To">以逗号分隔的收件人电子邮件地址列表。</param>
        /// <param name="Subject">电子邮件的主题行。</param>
        /// <param name="TargetUrl">目标Url，邮件打开后显示传入的Url对应的网页</param>
        public static void SendMail(string From, string To, string Subject, string TargetUrl)
        {
            HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(TargetUrl);
            HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
            System.IO.StreamReader SR = new StreamReader(HttpWResp.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
            string Body = SR.ReadToEnd();
            HttpWResp.Close();

            SendMail(From, To, Subject, true, Body, string.Empty, string.Empty);
        }


        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="From"></param>
        /// <param name="To"></param>
        /// <param name="Subject"></param>
        /// <param name="IsBodyHtml"></param>
        /// <param name="Body"></param>
        /// <param name="Cc"></param>
        /// <param name="Bcc"></param>
        public static void SendMail(string From, string To, string Subject, bool IsBodyHtml, string Body, string Cc, string Bcc)
        {
            MailMessage mailAudit = new MailMessage();
            mailAudit.From = new MailAddress(From); //目前发件为固定的; 
            mailAudit.To.Add(To);
            if (isEmail(Cc))
                mailAudit.CC.Add(Cc);
            if (isEmail(Bcc))
                mailAudit.Bcc.Add(Bcc);
            mailAudit.Subject = Subject;
            mailAudit.IsBodyHtml = IsBodyHtml;
            mailAudit.BodyEncoding = System.Text.Encoding.UTF8;
            mailAudit.Body = Body;
            Send(mailAudit);
        }

        public static void SendMail(string From, string To, string Subject, bool IsBodyHtml, string Body, string Cc, string Bcc, List<string> fileList)
        {
            MailMessage mailAudit = new MailMessage();
            mailAudit.From = new MailAddress(From); //目前发件为固定的; 
            mailAudit.To.Add(To);
            if (isEmail(Cc))
                mailAudit.CC.Add(Cc);
            if (isEmail(Bcc))
                mailAudit.Bcc.Add(Bcc);
            mailAudit.Subject = Subject;
            mailAudit.IsBodyHtml = IsBodyHtml;
            mailAudit.BodyEncoding = System.Text.Encoding.UTF8;
            mailAudit.Body = Body;

            //System.Web.Mail.MailAttachment attach = new System.Web.Mail.MailAttachment(file, System.Web.Mail.MailEncoding.Base64);
            foreach (var aFile in fileList)
            {
                mailAudit.Attachments.Add(new Attachment(aFile));
            }

            Send(mailAudit);
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="From"></param>
        /// <param name="To"></param>
        /// <param name="Subject"></param>
        /// <param name="IsBodyHtml"></param>
        /// <param name="Body"></param>
        /// <param name="Cc"></param>
        /// <param name="Bcc"></param>
        public static void SendMail(string From, string To, string Subject, bool IsBodyHtml, string Body, string Cc, string Bcc, out string messageID)
        {
            messageID = LumiSoft.Net.MIME.MIME_Utils.CreateMessageID(); //生成邮件唯一标识

            MailMessage mailAudit = new MailMessage();
            mailAudit.From = new MailAddress(From); //目前发件为固定的; 
            mailAudit.To.Add(To);
            if (isEmail(Cc))
                mailAudit.CC.Add(Cc);
            if (isEmail(Bcc))
                mailAudit.Bcc.Add(Bcc);
            mailAudit.Subject = Subject;
            mailAudit.IsBodyHtml = IsBodyHtml;
            mailAudit.BodyEncoding = System.Text.Encoding.UTF8;
            mailAudit.Body = Body;
            Send(mailAudit);
        }

        /// <summary>
        /// 验证邮件格式
        /// </summary>
        /// <param name="inputEmail"></param>
        /// <returns></returns>
        public static bool isEmail(string inputEmail)
        {
            string strRegex = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 发邮件
        /// </summary>
        /// <param name="mm"></param>
        /// <returns>发送成功标识</returns>
        public static bool Send(MailMessage mm)
        {
            if (mm == null) return false;
            try
            {
                if (mm.From.Address.Trim().Length == 0)
                {
                    mm.From = new MailAddress("info@aoyou.com"); //目前发件为固定的;
                }

                //mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //需要身份验证
                //mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "e-service@cytsonline.com"); //请不要修改
                //mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "1q2w3e4r5t"); //请不要修改
                //mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
                ////mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                //SmtpMail.SmtpServer = "smtp.cytsonline.com"; // 请不要修改
                //SmtpMail.Send(mm);

                System.Net.Mail.SmtpClient sc = new SmtpClient();
                sc.Credentials = new NetworkCredential("e-service", "1q2w3e4r5t", "CYTSINTRA");
                sc.DeliveryMethod = SmtpDeliveryMethod.Network;
                sc.Host = "cyts-mail.cytsonline.com";
                sc.Send(mm);
                return true;
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                return false;
            }
        }
    }
}
